#include <stdio.h>
#include <pcap.h>

#include "global.h"
#include "arp.h"

// Generate ARP reply packet
void arp_reply( eth_header *eh, arp_header *ah )
{
	int length = sizeof(eth_header) + sizeof(arp_header);  // Send packet length
	eth_header *reply_eh;
	arp_header *reply_ah;

	memset( v4SendBuffer, 0, 1600 );
	reply_eh = (eth_header *)( v4SendBuffer );
	reply_ah = (arp_header *)( v4SendBuffer + sizeof(eth_header));

	// Build Ethernet header
	int i;
	// Fill dst MAC
	for( i =0; i < 6; i++ )
		reply_eh->dmac[i] = eh->smac[i];
	// Fill src MAC with gateway MAC
	for( i =0; i < 6; i++ )
		reply_eh->smac[i] = v6GatewayMAC[i];
	reply_eh->type = eh->type;

	// Build ARP header
	reply_ah->hw_type = ah->hw_type;
	reply_ah->prot_type = ah->prot_type;
	reply_ah->hw_size = ah->hw_size;
	reply_ah->prot_size = ah->prot_size;
	reply_ah->option = htons(ARP_REPLY);
	// Fill src mac and ip
	for( i =0; i < 6; i++ )
		reply_ah->smac[i] = v6GatewayMAC[i];
	for( i =0; i < 4; i++ )
		reply_ah->sip[i] = ah->dip[i];
	for( i =0; i < 6; i++ )
		reply_ah->dmac[i] = ah->smac[i];
	for( i =0; i < 4; i++ )
		reply_ah->dip[i] = ah->sip[i];
	
	// Done

	/* Send down the packet */
	if (pcap_sendpacket(v4Handle, v4SendBuffer, length /* size */) != 0)
	{
		printf("v4 device : Error sending the packet: %s\n", pcap_geterr(v4Handle));
	}
	return;
}